package com.ysy.groovy.plugin

import com.android.build.api.transform.DirectoryInput
import com.android.build.api.transform.Format;
import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformException
import com.android.build.api.transform.TransformInput
import com.android.build.api.transform.TransformInvocation
import com.android.build.api.transform.TransformOutputProvider
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.utils.FileUtils
import com.ysy.java.plugin.MyClassVistor
import groovy.io.FileType
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter

class TestGroovyTransfrom extends Transform{

    /**
     * task  名字
     * @return
     */
    @Override
    public String getName() {
        return "TestTransfrom";
    }

    /**
     * 检索类型限定
     * @return
     */
    @Override
    public Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS;
    }

    /***
     *  限定范围
     * @return
     */
    @Override
    public Set<? super QualifiedContent.Scope> getScopes() {
        return TransformManager.PROJECT_ONLY;
    }

    /**
     * 是否需要增量更新
     * @return
     */
    @Override
    public boolean isIncremental() {
        return false;
    }

    @Override
    public void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
        super.transform(transformInvocation);
        System.out.println("transform==")
        Collection<TransformInput> inputs = transformInvocation.getInputs();//获取所有class 文件
        TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
        if (outputProvider!=null) {
            outputProvider.deleteAll();
        }
        inputs.each {TransformInput transformInput ->
            //遍历DirectoryInput（文件夹中的class 文件） directoryInputs 代表着以源码方式参与项目编译
            //比如我们手写的类以及R.class buildconfig.class 以及MainActivity。class等
            transformInput.getDirectoryInputs().each { DirectoryInput directoryInput ->
                File file = directoryInput.file
                if (file){
                    file.traverse (type: FileType.FILES,nameFilter:~/.*\.class/){ File itemFile ->
                        def name = itemFile.name
                        if (name != "BuildConfig.class" && !name.startsWith("R\$") ){
                            System.out.println("itemFile=="+name)
                            ClassReader reader=new ClassReader(itemFile.bytes)
                            ClassWriter writer = new ClassWriter(reader,ClassWriter.COMPUTE_MAXS)
                            //
                            MyClassVistor vistor = new MyClassVistor(writer)
                            reader.accept(vistor,ClassReader.EXPAND_FRAMES)

                            def array = writer.toByteArray()

                            def stream = new FileOutputStream(itemFile.path)
                            stream.write(array)
                            stream.close()
                        }
                    }
                }
                //出来玩输入文件 把输出传给下一个文件
                def dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
                FileUtils.copyDirectory(directoryInput.file,dest)
            }
        }
    }
}